package com.eims.sls_app.ui;

import java.util.ArrayList;

import com.contans.view.MainFenxiVo;
import com.eims.ball.info.adapter.MyFragmentPagerAdapter;
import com.eims.ball.info.fragment.FenxiOneFragment;
import com.eims.ball.info.fragment.PeilvTwoFragment;
import com.eims.sls_app.dataaccess.Lottery;
import com.eims.sls_app.dataaccess.Schemes;
import com.eims.sls_app.dataaccess.Users;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.ImageManager;
import com.eims.sls_app.view.XCRoundImageView;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import bls.juejin.cn.R;

public class MainFenixActivity extends MyFragmentActivity implements OnClickListener {
	private String TAG = "MainFaHongbaoActivity";
	private ViewPager mPager;
	private ImageView ivBottomLine;
	private TextView tvTabActivity, tvTabGroups;
	private ArrayList<Fragment> fragmentsList;
	private int currIndex = 0;
	private int bottomLineWidth;
	private int offset = 0;
	private int position_one;
	private int position_two;
	private int position_three;
	private Resources resources;
	private MyFragmentPagerAdapter adaper;

	private ImageButton ibBack;

	private TextView tvTitle, tvMainName, tvMainRank, tvGuesName, tvGuesRank, tvCenterInfo, tvCenterTime;
	private XCRoundImageView ivMianBg, ivGuesBg;

	private MainFenxiVo mainFenxiVo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_fenxi_layout);
		resources = getResources();
		mainFenxiVo = App.mainFenxiVo;
		InitWidth();
		InitTextView();
		InitViewPager();
	}

	@Override
	public Resources getResources() {
		// TODO Auto-generated method stub
		Resources res = super.getResources();
		Configuration config = new Configuration();
		config.setToDefaults();
		res.updateConfiguration(config, res.getDisplayMetrics());
		return res;
	}

	private void InitTextView() {
		Log.e("11", "3");
		tvTabActivity = (TextView) findViewById(R.id.tv_tab_activity);
		tvTabGroups = (TextView) findViewById(R.id.tv_tab_groups);
		ibBack = (ImageButton) findViewById(R.id.btn_back_con);
		ibBack.setOnClickListener(this);
		tvTabActivity.setOnClickListener(new MyOnClickListener(0));
		tvTabGroups.setOnClickListener(new MyOnClickListener(1));

		tvTitle = (TextView) findViewById(R.id.tv_title);
		tvMainName = (TextView) findViewById(R.id.tv_main_name);
		tvMainRank = (TextView) findViewById(R.id.tv_main_rank);
		tvGuesName = (TextView) findViewById(R.id.tv_gues_name);
		tvGuesRank = (TextView) findViewById(R.id.tv_gues_rank);
		tvCenterInfo = (TextView) findViewById(R.id.tv_center_info);
		tvCenterTime = (TextView) findViewById(R.id.tv_center_time);

		ivMianBg = (XCRoundImageView) findViewById(R.id.iv_main_bg);
		ivGuesBg = (XCRoundImageView) findViewById(R.id.iv_gues_bg);

		tvMainName.setText(mainFenxiVo.getRow().getHn());
		tvMainRank.setText(mainFenxiVo.getRow().getHomeRank());
		tvGuesName.setText(mainFenxiVo.getRow().getGn());
		tvGuesRank.setText(mainFenxiVo.getRow().getGuestRank());
		tvCenterTime.setText(mainFenxiVo.getRow().getTime().split(" ")[1]);
		ImageManager.getInstance().displayImage(
				"http://01link.igowu123.com/newzlk/img/team/team_" + mainFenxiVo.getRow().getHid() + ".jpg", ivMianBg,
				ImageManager.getViewsHeadOptions());
		ImageManager.getInstance().displayImage(
				"http://01link.igowu123.com/newzlk/img/team/team_" + mainFenxiVo.getRow().getGid() + ".jpg", ivGuesBg,
				ImageManager.getViewsHeadOptions());

		tvTitle.setText(mainFenxiVo.getRow().getLeaguename());

	}

	private void InitViewPager() {

		Log.e("11", "4");
		mPager = (ViewPager) findViewById(R.id.vPager);
		fragmentsList = new ArrayList<Fragment>();

		if (fragmentsList != null) {
			fragmentsList.clear();
			Log.e("11", "14");
		}
		// Fragment activityfragment = TestFragment.newInstance("Hello",paramMap);
		// Fragment groupFragment = TestFragment.newInstance("Hello Group.",paramMap2);

		//
		// fragmentsList.add(activityfragment);
		// fragmentsList.add(groupFragment);

		Fragment activityfragment = FenxiOneFragment.newInstance(mainFenxiVo.getRow().getItemId(),
				mainFenxiVo.getRow().getHid(), mainFenxiVo.getRow().getGid(),mainFenxiVo.getRow().getHn(),mainFenxiVo.getRow().getGn());
		Fragment groupFragment =PeilvTwoFragment.newInstance(mainFenxiVo.getRow().getItemId(),
				mainFenxiVo.getRow().getHid(), mainFenxiVo.getRow().getGid(),mainFenxiVo.getRow().getHn(),mainFenxiVo.getRow().getGn());

		fragmentsList.add(activityfragment);
		fragmentsList.add(groupFragment);

		adaper = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList);
		adaper.notifyDataSetChanged();
		mPager.setAdapter(adaper);

		mPager.setCurrentItem(0);
		mPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		fragmentsList.clear();
		Log.e("1", "5");
		super.onDestroy();
	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		Log.e("1", "6");
		super.onStop();
	}

	@Override
	public void onPause() {
		Log.e("1", "7");
		// TODO Auto-generated method stub
		super.onPause();
	}

	private void InitWidth() {
		Log.e("11", "2");
		ivBottomLine = (ImageView) findViewById(R.id.iv_bottom_line);
		int h = MainFenixActivity.this.getResources().getDisplayMetrics().heightPixels;
		int w = MainFenixActivity.this.getResources().getDisplayMetrics().widthPixels;
		LayoutParams lp;
		lp = (LayoutParams) ivBottomLine.getLayoutParams();
		lp.width = w / 2;
		ivBottomLine.setLayoutParams(lp);

		bottomLineWidth = ivBottomLine.getLayoutParams().width;
		Log.d(TAG, "cursor imageview width=" + bottomLineWidth);
		DisplayMetrics dm = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		offset = (int) ((screenW / 2.0 - bottomLineWidth) / 2);

		position_one = (int) (screenW / 2.0);
		position_two = position_one * 2;

	}

	public class MyOnClickListener implements View.OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			mPager.setCurrentItem(index);
		}
	};

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int arg0) {
			Animation animation = null;
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					animation = new TranslateAnimation(position_one, 0, 0, 0);
					tvTabGroups.setTextColor(resources.getColor(R.color.main_peilv));
				}
				tvTabActivity.setTextColor(resources.getColor(R.color.main_red));
				break;
			case 1:
				if (currIndex == 0) {
					animation = new TranslateAnimation(0, position_one, 0, 0);
					tvTabActivity.setTextColor(resources.getColor(R.color.main_peilv));
				}
				tvTabGroups.setTextColor(resources.getColor(R.color.main_red));
				break;

			}
			currIndex = arg0;
			animation.setFillAfter(true);
			animation.setDuration(300);
			ivBottomLine.startAnimation(animation);
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_back_con:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		Log.e("xxxa", "tiqu---------");

		outState.putString("lotterysId", App.lotterysId);

		outState.putInt("schemeId", App.schemeId);

		outState.putSerializable("user", App.user);
		outState.putSerializable("mainFenxiVo", App.mainFenxiVo);

		outState.putSerializable("scheme", App.scheme);
		outState.putSerializable("lottery", App.lottery);

		super.onSaveInstanceState(outState);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		Log.e("xxxa", "tiqu---------11");
		if (savedInstanceState != null) {

			App.lotterysId = savedInstanceState.getString("lotterysId");

			App.schemeId = savedInstanceState.getInt("schemeId");

			App.scheme = (Schemes) savedInstanceState.getSerializable("scheme");
			App.user = (Users) savedInstanceState.getSerializable("user");
			App.mainFenxiVo = (MainFenxiVo) savedInstanceState.getSerializable("mainFenxiVo");

			App.lottery = (Lottery) savedInstanceState.getSerializable("lottery");

		}
	}

}
